﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RealEstateApp.Library;
using RealEstateApp.WebUI.Models;

namespace RealEstateApp.WebUI.Controllers
{ 
    public class EmployeeController : Controller
    {
        private RealEasteDBEntities db = new RealEasteDBEntities();

        //
        // GET: /Employee/

        public ViewResult Index()
        {
            var query = from c in db.Customers
                        join c_r in db.Customer_CustomerRoles on c.CustID equals c_r.CustomerFID
                        join r in db.CustomerRoles on c_r.CustomerRoleFID equals r.CustomerRoleID
                        where r.Name == "Employees"
                        select c;
            return View(query.ToList());
        }

        //
        // GET: /Employee/Details/5

        public ViewResult Details(int id)
        {
            Customer customer = db.Customers.Find(id);
            return View(customer);
        }

        //
        // GET: /Employee/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult Create(CustomerModel model)
        {
            if (ModelState.IsValid)
            {
                var customer = new Customer
                {
                    CustName = model.Name,
                    BirthDate = model.BirthDate,
                    Phone = model.Phone,
                    Mobile = model.Mobile,
                    Email = model.Email,
                    Address = model.Address,
                    CreatedBy = 1
                };


                db.Customers.Add(customer);
                db.SaveChanges();

                //customer roles
                db.Customer_CustomerRoles.Add(new Customer_CustomerRoles { CustomerFID = customer.CustID, CustomerRoleFID = 2 });
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            

            return View(model);
        }
        
        //
        // GET: /Employee/Edit/5
 
        public ActionResult Edit(int id)
        {
            Customer customer = db.Customers.Find(id);

            var model = new CustomerModel
            {
                CustomerId = customer.CustID,
                Name = customer.CustName,
                BirthDate = customer.BirthDate,
                Phone = customer.Phone,
                Mobile = customer.Mobile,
                Email = customer.Email,
                Address = customer.Address
            };
            var tasks = db.Tasks.Where(x => x.EmployeeFID == model.CustomerId).ToList();
            model.Tasks = tasks;
            return View(model);
        }

        //
        // POST: /Employee/Edit/5

        [HttpPost]
        public ActionResult Edit(CustomerModel model)
        {
            if (ModelState.IsValid)
            {
                var customer = db.Customers.Find(model.CustomerId);

                customer.CustName = model.Name;
                customer.BirthDate = model.BirthDate;
                customer.Phone = model.Phone;
                customer.Mobile = model.Mobile;
                customer.Address = model.Address;
                db.Entry(customer).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }

        //
        // Get: /Customer/Edit/5

        public PartialViewResult ListTask(int employeeId)
        {
            //var customer = db.Customers.Find(customerId);
            var tasks = db.Tasks.Where(x => x.EmployeeFID == employeeId).ToList();

            var model = new CustomerModel
            {
                CustomerId = employeeId,
                Tasks = tasks
            };
            return PartialView(model);
        }

        //
        // GET: /Employee/Delete/5
 
        public ActionResult Delete(int id)
        {
            Customer customer = db.Customers.Find(id);
            return View(customer);
        }

        //
        // POST: /Employee/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Customer customer = db.Customers.Find(id);
            db.Customers.Remove(customer);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}